<?php

/**
 * This is the model class for table "sd.property_value".
 *
 * The followings are the available columns in table 'sd.property_value':
 * @property string $id
 * @property string $property_id
 * @property string $value
 *
 * The followings are the available model relations:
 * @property PropertyValue $parent
 * @property PropertyValue[] $propertyValues
 * @property Property $property
 */
class PropertyValue extends CBaseActiveRecord
{
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'sd.property_value';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('property_id, value', 'required'),
			array('property_id', 'length', 'max'=>10),
			array('value', 'length', 'max'=>45),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('id, property_id, value', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'propertyValues' => array(self::HAS_MANY, 'PropertyValue', 'parent_id'),
			'property' => array(self::BELONGS_TO, 'Property', 'property_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'property_id' => 'Property',
			'value' => 'Value',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id,true);
		$criteria->compare('property_id',$this->property_id,true);
		$criteria->compare('value',$this->value,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return PropertyValue the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	public function propertyId($id)
	{
		if($id < 1)
			return $this;

		$this->getDbCriteria()->addInCondition('property_id', (array)$id);

		return $this;
	}

	public function saveRow($propertyId, $values, $values_id = array())
	{
		if($propertyId < 1)
			return array();

		$oRow = null;

//		print_r($values);
//		print_r($values_id); exit;

		foreach($values as $i => $title)
		{
			$filter = array(
				'property_id' => $propertyId,
				'value' => $title,
			);

			if(isset($values_id[$i]) && mb_strlen($title) > 0)
			{
				unset($values_id[$i]);
			}

			$oRow = $this->findByAttributes($filter);

			if(!$oRow)
				$oRow = new self;

			$oRow->setAttributes(array(
			                          'property_id' => $propertyId,
			                          'value' => $title,
			                     ));

			$oRow->save();
		}

		if(count($values_id))
		{
			foreach($values_id as $id)
				$this->deleteByPk($id);
		}

		//print_r($params); exit;

		return $oRow;
	}

	public function selectByLangedRow(&$oTargetRow, $key = 'parent_id')
	{
		if(!$oTargetRow || !is_array($oTargetRow))
			return $oTargetRow;

		$oRow = current($oTargetRow);

		if($oRow->id < 1)
			return $oTargetRow;

		$ids = array();
		foreach($oTargetRow as $oRow)
			$ids[$oRow->$key] = $oRow->$key;

		$oCriteria = $this->getDbCriteria();
		$oCriteria->addInCondition('property_id', $ids);

		$oRowset = $this->findAll($oCriteria);
		$aRowset = array();

		foreach($oRowset as $oRow)
		{
			$aRowset[$oRow->property_id][$oRow->lang][$oRow->id] = $oRow;
		}

		foreach($oTargetRow as $key => $oRow)
		{
			if(isset($aRowset[$oRow->parent_id]) && isset($aRowset[$oRow->parent_id][$oRow->lang]))
			$oTargetRow[$key]->setValues($aRowset[$oRow->parent_id][$oRow->lang]);
		}

		return $oRowset;
	}

    public function findByValue($value, $type)
    {
        $oCriteria = $this->getDbCriteria();
        $oCriteria->join = 'INNER JOIN property p ON p.id = t.property_id';
        $oCriteria->addSearchCondition('t.value', $value);
        $oCriteria->addColumnCondition(array('p.type' => $type));

        $oRow = $this->find($oCriteria);

        return $oRow;
        print_r($oRow); exit;
    }

}
